package com.jxtti.controller;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jxtti.entity.NetworkSurvey;
import com.jxtti.service.NetworkSurveyService;
import com.jxtti.utils.ExceptionUtil;
/**
 * 
 * <br>
 * <b>功能：</b>NetworkSurveyController<br>
 * <b>作者：lwb</b><br>
 * <b>日期：</b> Nov 26, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，<br>
 */ 
@Controller
@RequestMapping("/networkSurvey") 
public class NetworkSurveyController {
	
	private final static Logger log= Logger.getLogger(NetworkSurveyController.class);
	
	// Servrice start
	@Autowired
	private NetworkSurveyService  networkSurveyService; 
	
	
	
	
	
	/**
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/list") 
	public ModelAndView  list(@RequestParam Map<String, Object> data,  NetworkSurvey networkSurvey, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/interaction/networkSurvey_main.jsp");
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return mav;
	}
	
	
	
	@RequestMapping(value="/listData")
	@ResponseBody
	public List<NetworkSurvey>  listData(@RequestParam Map<String, Object> data,  NetworkSurvey networkSurvey, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		List<NetworkSurvey> lst = this.networkSurveyService.queryAll(data);
		return lst;
	}
	
	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/interaction/networkSurvey_Add.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			NetworkSurvey  networkSurvey, HttpServletRequest request,
			HttpServletResponse response) throws Exception{

		ModelAndView mav = new ModelAndView();
		try {
			// 保存数据
			int exeCount = networkSurveyService.add(networkSurvey);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	
	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, NetworkSurvey  networkSurvey,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			networkSurvey = networkSurveyService.queryNetworkSurveyById(networkSurvey);
		
			data.put("networkSurvey", networkSurvey);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/interaction/networkSurvey_edit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	
	
	
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			NetworkSurvey  networkSurvey, HttpServletRequest request,
			HttpServletResponse response) {
		try {
		//	User user = AuthUtil.getUserInfo(request);
		//	networkSurvey.setOperPerson(user.getUserName());
			// 修改
			NetworkSurvey networkSurveyTem=networkSurveyService.queryNetworkSurveyById(networkSurvey);

			
			int exeCount = networkSurveyService.update( networkSurvey);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	

/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			NetworkSurvey  networkSurvey, HttpServletRequest request,
			HttpServletResponse response) {
		try {
		//	User user = AuthUtil.getUserInfo(request);
		//	networkSurvey.setOperPerson(user.getUserName());
		
			 networkSurvey=networkSurveyService.queryNetworkSurveyById( networkSurvey);
			int exeCount = networkSurveyService.delete(networkSurvey);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

}
